package com.weaver.seconddev.api.tl.gr_api;

/**
 * E9原始类路径:com.api.tl.service.EcologyCommonService
 * JAVA替换说明文档:https://ncnb71xwgeda.feishu.cn/wiki/JLsjwxK8xiHuPIkdu0Dc27Rlnaf?from=from_copylink
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.esb.api.entity.EsbResponseEntity;
import com.weaver.esb.api.entity.EventParams;
import com.weaver.esb.api.service.EsbActionFlow;
import com.weaver.framework.rpc.context.impl.TenantRpcContext;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.file.Prop;
import com.weaver.verupgrade.general.Util;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

/**
 * 泛微平台提供接口调用
 *
 * @author th
 * @date 2023/10/19 14:16
 */
@org.springframework.stereotype.Service("tl_service_EcologyCommonService2")
public class EcologyCommonService2 {

    public static final Logger logger_fd84fed2 = LoggerFactory.getLogger(EcologyCommonService2.class);

    @Autowired
    private static EsbActionFlow esbActionFlow;

    /**
     * 调用泛微平台ESB接口
     *
     * @param eventKey
     * @param requestMessage
     * @return
     */
    public static String callESBInterface(String eventKey, String requestMessage) {
        logger_fd84fed2.info(String.valueOf("======Start Call ESB Interface======eventKey:" + eventKey + ",requestMessage:" + requestMessage));
        try {
            long sendReqTime = System.currentTimeMillis();
            // 调用ESB接口
            // 获取 ESB 服务
    /*        EsbService service = EsbClient.getService();
            String result = service.execute(eventKey, requestMessage);*/
            String tenantKey = TenantRpcContext.getTenantKey();
            String eteamsId = TenantRpcContext.getEteamsId();
            String employeeId = TenantRpcContext.getEmployeeId();
            EventParams eventParams = new EventParams();
            eventParams.setEventId(Long.valueOf(eventKey));
            eventParams.setEmployeeId(employeeId);
            eventParams.setSource("workflow");
            Map<String, Object> customParams = JSON.parseObject(requestMessage, Map.class);
            eventParams.setCustomParams(customParams);

            EsbResponseEntity esbResponseEntity = esbActionFlow.customTriggerEsbFlow(eventParams,tenantKey);
            String resultMsg = esbResponseEntity.getResultMsg();
            int resultCode = esbResponseEntity.getResultCode();
            Object resultData = esbResponseEntity.getResultData();
            long returnRspTime = System.currentTimeMillis();

            logger_fd84fed2.info(String.valueOf("Complete Call ESB Interface,eventKey:" + eventKey + ",Time:" + String.format("%.2f", (returnRspTime - sendReqTime) / 1000.0) + "s,Result:"+resultData ));
            logger_fd84fed2.info("resultMsg---->"+resultMsg+"---resultCode:"+resultCode);
             //long code = JSON.parseObject(result).getIntValue("code");
           if (resultCode == 100) {
                return resultData.toString();
            } else {
                logger_fd84fed2.info(String.valueOf("Call ESB Interface Error Code:" + resultCode));
                throw new Exception();
            }
        } catch (Exception e) {
            logger_fd84fed2.error("callESBInterface exception");
            throw new RuntimeException("callESBInterface exception", e);
        }
    }
}
